﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace AjaxControlFramework
{
    public class JsonStateStrategy : StateStrategy
    {
        //------// Properties \\--------------------------------------------\\

        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public JsonStateStrategy(Control targetControl)
            : base(targetControl)
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        protected override void PersistState()
        {
            if (base.ViewState != null || base.ControlState != null)
            {
                // Serialize both the page ControlState and ViewState.
                string controlState = base.StateFormatter.Serialize(new Pair(base.ViewState, base.ControlState));

                Control.AjaxContext.State["ControlState"] = controlState;
            }
        }


        protected override void RestoreState()
        {
            object controlState = null;
            Control.AjaxContext.State.TryGetValue("ControlState", out controlState);

            if (controlState != null && (string)controlState != null && (controlState as string) != String.Empty)
            {
                Pair statePair = (Pair)base.StateFormatter.Deserialize((string)controlState);

                base.ViewState = statePair.First;
                base.ControlState = statePair.Second;
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}